/*
 * Copyright 2016-2021 Pnoker. All Rights Reserved.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.ac.driver.mqtt.service.impl;

import com.ac.driver.mqtt.service.StateService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xj.common.bean.point.PointValue;
import com.xj.common.sdk.service.DriverService;
import com.xj.common.sdk.service.PointValueService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author pnoker
 */
@Slf4j
@Service
public class StateServiceImpl implements StateService {

    @Resource
    private DriverService driverService;

    @Resource
    private PointValueService pointValueService;
    @Override
    public Boolean doChargeState(long deviceId,String stateMsg) {
        String[] datas = stateMsg.split(";");
        Map<String,String> dataMap =  new HashMap<>();
        for(String data: datas){
            if(data!=null&&data.trim()!=""){
                String[] keyvalue = data.split(":");
                dataMap.put(keyvalue[0],keyvalue[1]);
            }
        }
        double I = Double.parseDouble(dataMap.get("I"));
        double U = Double.parseDouble(dataMap.get("U"));
        double P = Double.parseDouble(dataMap.get("P"));
        double KW = Double.parseDouble(dataMap.get("kW"));
        double F = Double.parseDouble(dataMap.get("F"));
        double COS = Double.parseDouble(dataMap.get("COS"));
        double T1 = Double.parseDouble(dataMap.get("T1"));
        double T2 = Double.parseDouble(dataMap.get("T2"));
        List<PointValue> listPointV= new ArrayList<>();
        listPointV.add(pointValueService.getPointValue(deviceId,"I",I,false,"A"));
        listPointV.add(pointValueService.getPointValue(deviceId,"U",U,false,"V"));
        listPointV.add(pointValueService.getPointValue(deviceId,"P",P,false,"W"));
        listPointV.add(pointValueService.getPointValue(deviceId,"kW",KW,false,"KW.H"));
        listPointV.add(pointValueService.getPointValue(deviceId,"F",F,false,"HZ"));
        listPointV.add(pointValueService.getPointValue(deviceId,"COS",COS,false,"°C"));
        listPointV.add(pointValueService.getPointValue(deviceId,"T1",T1,false,"T1"));
        listPointV.add(pointValueService.getPointValue(deviceId,"T2",T2,false,"T2"));

        driverService.pointValueSender(listPointV);
        return true;
    }
}
